package org.gwu.seuss.communication.sockets.ssl;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Properties;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;

public class Util {

	public static final String[] CIPHERSUITE = {"TLS_RSA_WITH_AES_256_CBC_SHA"};
	
	public static Properties loadPropertiesFromResource(String resourceName) throws IOException{
			URL url = Util.class.getClassLoader().getResource(resourceName);
			Properties properties = new Properties();
			properties.load(url.openStream());
			return properties;
	}
	
	public static ManagerPair<KeyManagerFactory,KeyStore> getInitKeyFactory(InputStream inputStream, String password, String keyStoreType) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException {
		
		  KeyStore keyStore = KeyStore.getInstance(keyStoreType);
		  keyStore.load(inputStream, password.toCharArray());
		  KeyManagerFactory factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
		  factory.init(keyStore, password.toCharArray());

		  return new ManagerPair<KeyManagerFactory, KeyStore>(factory,keyStore);  
	}
	
	public static TrustManagerFactory getInitTrustFactory(KeyStore keyStore) throws NoSuchAlgorithmException, KeyStoreException{
		
	    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(keyStore);
        return tmf;
	}
	
}
